package com.clkj.sunhelp.mvp.seekhelpnew.list;

import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;

import com.clkj.sunhelp.mvp.seekhelpnew.QzrServices;
import com.clkj.sunhelp.mvp.seekhelpnew.domain.QzxxListItem;
import com.clkj.sunhelp.util.schedulers.BaseSchedulerProvider;

import java.util.ArrayList;
import java.util.List;

import rx.subscriptions.CompositeSubscription;

/**
 * Created by Administrator on 2017/3/30.
 */

public class SeekHelpListPresenter implements SeekHelpListContract.Presenter {

    @NonNull
    SeekHelpListContract.View mView;

    @NonNull
    private final BaseSchedulerProvider mSchedulerProvider;


    @NonNull
    private CompositeSubscription mSubscriptions;

    @NonNull
    private Handler mDealHandler;


    //数据类型:没有更多,有数据
    public static final int TYPE_DATA_NO_MORE = 1;
    public static final int TYPE_DATA_HAS_DATA = 2;
    //异常
    public static final int TYPE_DATA_EXCEPTION = 3;

    public SeekHelpListPresenter(@NonNull SeekHelpListContract.View view, @NonNull BaseSchedulerProvider mSchedulerProvider
            , @NonNull Handler mdealHandler) {
        this.mView = view;
        this.mSchedulerProvider = mSchedulerProvider;

        this.mSubscriptions = new CompositeSubscription();

        this.mDealHandler = mdealHandler;

        this.mView.setPresenter(this);
    }



    /*@Override
    public void getPolicyKind() {

    }*/

    @Override
    public void subscribe() {

    }

    @Override
    public void unsubscribe() {
        mSubscriptions.unsubscribe();
    }

    @Override
    public void getQiuZhuInfoList(final int offset, final int maxSize, final String params) {


        new Thread(new Runnable() {
            @Override
            public void run() {
                List<QzxxListItem> qzrs = new ArrayList<QzxxListItem>();
                try {
                    qzrs = QzrServices.getScrollData(offset,
                            maxSize, params);



                } catch (Exception e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();

                    Message message = new Message();
                    message.what = TYPE_DATA_EXCEPTION;
                    mDealHandler.sendMessage(message);
                }
                Message message = new Message();

                if (null == qzrs || qzrs.isEmpty()) {
                    /*Toast toast = Toast.makeText(getApplicationContext(), "没有更多数据!",
                            Toast.LENGTH_LONG);
                    // 起点位置,水平向右位移,垂直向下位移
                    toast.setGravity(Gravity.TOP, 0, 220);
                    toast.show();*/

                    message.what = TYPE_DATA_NO_MORE;
                    mDealHandler.sendMessage(message);

                }else {
                    message.what = TYPE_DATA_HAS_DATA;
                    message.obj = qzrs;
                    mDealHandler.sendMessage(message);
                }


            }
        }).start();




    }





    /*@Override
    public void loginPeople(String account, String pass) {
        mView.showLoading();

        Subscription subscription = mHttpService.loginOfPeople(account,pass)
                .subscribeOn(mSchedulerProvider.computation())
                .observeOn(mSchedulerProvider.ui())
                .filter(new Func1<JsonObject, Boolean>() {
                    @Override
                    public Boolean call(JsonObject jsonObject) {
                        LogUtil.e("loginResponse:"+ jsonObject.toString());
                        if (!jsonObject.get(Constant.RESPONSE_KEY_STATUS).getAsString().equals(Constant.SUCCESS))
                        {
                            mView.dismissLoading();
                            mView.onLoginPeopleError(jsonObject.get(Constant.RESPONSE_KEY_MESSAGE).getAsString());
                        }

                        return jsonObject.get(Constant.RESPONSE_KEY_STATUS).getAsString().equals(Constant.SUCCESS);
                    }
                })
                .subscribe(new Observer<JsonObject>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        mView.dismissLoading();
                        mView.onLoginPeopleError(TipStr.TIP_SERVER_GET_DATA_WRONG);
                    }

                    @Override
                    public void onNext(JsonObject jsonObject) {



                        mView.dismissLoading();

                        if (!jsonObject.get("data").isJsonNull())
                        {

                            //List<PolicyKindItem> policyKindList = CommonUtil.formatJsonToList(jsonObject,"data",new TypeToken<List<PolicyKindItem>>(){});

                            List<UserLoginInfo> userInfoList  = CommonUtil.formatJsonToList(jsonObject,"data",new TypeToken<List<UserLoginInfo>>(){});

                            mView.dismissLoading();
                            mView.onLoginPeopleSuccess(userInfoList.get(0));
                        }else{

                            mView.dismissLoading();
                            mView.onLoginPeopleError(TipStr.TIP_SERVER_GET_DATA_WRONG);

                        }

                    }
                });
        mSubscriptions.add(subscription);
    }

    @Override
    public void loginWorker(String account, String pass) {
        Subscription subscription = mHttpService.loginOfWorker(account,pass)
                .subscribeOn(mSchedulerProvider.computation())
                .observeOn(mSchedulerProvider.ui())
                .filter(new Func1<JsonObject, Boolean>() {
                    @Override
                    public Boolean call(JsonObject jsonObject) {
                        LogUtil.e("loginWorkerResponse:"+ jsonObject.toString());
                        if (!jsonObject.get(Constant.RESPONSE_KEY_STATUS).getAsString().equals(Constant.SUCCESS))
                        {
                            mView.dismissLoading();
                            mView.onLoginWorkerError(jsonObject.get(Constant.RESPONSE_KEY_MESSAGE).getAsString());
                        }

                        return jsonObject.get(Constant.RESPONSE_KEY_STATUS).getAsString().equals(Constant.SUCCESS);
                    }
                })
                .subscribe(new Observer<JsonObject>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        mView.dismissLoading();
                        mView.onLoginWorkerError(TipStr.TIP_SERVER_GET_DATA_WRONG);
                    }

                    @Override
                    public void onNext(JsonObject jsonObject) {



                        mView.dismissLoading();

                        if (!jsonObject.get("data").isJsonNull())
                        {

                            //List<PolicyKindItem> policyKindList = CommonUtil.formatJsonToList(jsonObject,"data",new TypeToken<List<PolicyKindItem>>(){});

                            List<WorkerLoginInfo> workerInfoList = CommonUtil.formatJsonToList(jsonObject,"data",new TypeToken<List<WorkerLoginInfo>>(){});

                            //WorkerLoginInfo workerLoginInfo = (WorkerLoginInfo) CommonUtil.formatJsonToObject(jsonObject,"data",new TypeToken<WorkerLoginInfo>(){});

                            mView.dismissLoading();
                            mView.onLoginWorkerSuccess(workerInfoList.get(0));
                        }else{

                            mView.dismissLoading();
                            mView.onLoginWorkerError(TipStr.TIP_SERVER_GET_DATA_WRONG);

                        }

                    }
                });
        mSubscriptions.add(subscription);
    }*/
}

